package com.mrduan.otgfilemanager.ui.aideo;

import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import com.github.mjdev.libaums.fs.UsbFile;
import com.hrb.library.MiniMusicView;
import com.mrduan.otgfilemanager.Config;
import com.mrduan.otgfilemanager.R;
import com.mrduan.otgfilemanager.ui.main.fragment.OneScreenFragment;
import com.orhanobut.logger.Logger;
import com.shike.baselibrary.activity.BaseActivity;
import com.shike.baselibrary.utils.SharepreferenceUtils;

import org.w3c.dom.Text;

import java.io.File;
import java.io.IOException;

public class AideoPlayActivity extends BaseActivity implements MiniMusicView.OnMusicStateListener, View.OnClickListener, SeekBar.OnSeekBarChangeListener {

    private MiniMusicView mMusicView;

    private TextView currentTime;// 现在播放时间
    private TextView totalTime;// 音频总时长
    private TextView singerName;// 歌手名字
    private TextView music_name;// 文件名

    private ImageView backIco;
    private ImageView play_or_pause;//播放，暂停
    private ImageView player_pre_btn;// 上一曲
    private ImageView player_next_btn;// 下一曲

    private ImageView music_thumbnail;

    private SeekBar mSeekBar;// 进度条

    //    private String filePath = ""; // 需要操作的文件路径
    private UsbFile[] files;  // 文件夹下的所有文件
    private int location;    // 保存当前播放位置


    @Override
    public int getContentView() {
        return R.layout.activity_aideo_play;
    }

    @Override
    public void initBindWidget() {
        mMusicView = findViewById(R.id.mmv_music);

        View view = View.inflate(this, R.layout.video_play, null);

        currentTime = view.findViewById(R.id.current_time);
        totalTime = view.findViewById(R.id.total_time);
        singerName = view.findViewById(R.id.singerName);
        music_name = view.findViewById(R.id.music_name);

        backIco = view.findViewById(R.id.back_ico);
        play_or_pause = view.findViewById(R.id.play_or_pause);
        player_pre_btn = view.findViewById(R.id.player_pre_btn);
        player_next_btn = view.findViewById(R.id.player_next_btn);

        music_thumbnail = view.findViewById(R.id.music_thumbnail);

        mSeekBar = view.findViewById(R.id.seek_bar);

        mMusicView.addView(view);
    }

    @Override
    public void initListener() {
        mMusicView.setOnMusicStateListener(this);

        backIco.setOnClickListener(this);
        play_or_pause.setOnClickListener(this);
        player_pre_btn.setOnClickListener(this);
        player_next_btn.setOnClickListener(this);

        mSeekBar.setOnSeekBarChangeListener(this);
    }

    @Override
    public void initData() throws IOException {

//        filePath = getIntent().getStringExtra("path");

        location = Integer.valueOf(SharepreferenceUtils.getString(this, Config.getPath(OneScreenFragment.usbFile), "0"));

//        File f = new File(filePath);
        files = OneScreenFragment.usbFile.listFiles();

        if (files.length > location) {
            setMusic(location);
        } else {
            location = 0;
            if (files.length > location) {
                setMusic(location);
            } else {
                Toast.makeText(this, "当前曲库无音乐！", Toast.LENGTH_SHORT).show();
            }
        }
    }

    public void setMusic(int i) {

        UsbFile f = files[i];

        mMusicView.startPlayMusic(Config.getUrl(f));

        String[] splits = f.getName().split("\\.");
        if (splits.length > 0) {
            music_name.setText(splits[0]);
        }

        play_or_pause.setTag("1");
        play_or_pause.setImageResource(R.drawable.search_stop_btn);

        SharepreferenceUtils.saveString(this, Config.getPath(OneScreenFragment.usbFile), i + "");
    }

    @Override
    protected void onDestroy() {
        if (mMusicView.isPlaying())
            mMusicView.stopPlayMusic();
        super.onDestroy();
    }

    private void play_or_pause(View view) {// 播放，暂停
        if (play_or_pause.getTag().toString().equals("0")) {// 当前为暂停状态，修改为播放状态
            play_or_pause.setTag("1");
            play_or_pause.setImageResource(R.drawable.search_stop_btn);
            if (totalTime.getText().equals("00:00")) {
                setMusic(location);
            } else {
                mMusicView.resumePlayMusic();
            }
        } else {
            play_or_pause.setTag("0");
            play_or_pause.setImageResource(R.drawable.ring_btnplay);
            if (mMusicView.isPlaying())
                mMusicView.pausePlayMusic();
        }
    }

    private void player_pre_btn(View view) {// 上一曲
        if (location != 0) {
            setMusic(--location);
        } else {
            Toast.makeText(this, "当前是第一曲！", Toast.LENGTH_SHORT).show();
        }
    }

    private void player_next_btn(View view) {// 下一曲
        if (location + 1 == files.length) {
            Toast.makeText(this, "当前是最后一曲！", Toast.LENGTH_SHORT).show();
        } else {
            setMusic(++location);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back_ico:
                finish();
                break;
            case R.id.play_or_pause:
                play_or_pause(view);
                break;
            case R.id.player_pre_btn:
                player_pre_btn(view);
                break;
            case R.id.player_next_btn:
                player_next_btn(view);
                break;
        }
    }

    /*-------------------OnMusicStateListener音乐播放器监听-------------------------------*/
    @Override
    public void onPrepared(int duration) {
        mSeekBar.setMax(duration);
        totalTime.setText(Config.secondToMin(duration));
    }

    @Override
    public void onError(int what, int extra) {

    }

    @Override
    public void onInfo(int what, int extra) {
    }

    @Override
    public void onMusicPlayComplete() {
        player_next_btn(null);
    }

    @Override
    public void onSeekComplete() {
    }

    @Override
    public void onProgressUpdate(int duration, int currentPos) {
        mSeekBar.setProgress(currentPos);
        currentTime.setText(Config.secondToMin(currentPos));
    }

    @Override
    public void onHeadsetPullOut() {// 如果耳机拔出的时候正在播放
        if (play_or_pause.getTag().toString().equals("1")) {// 暂停音乐
            play_or_pause(null);
        }
    }


    private int point;// 记录移动开始时的位置

    /*---------------OnSeekBarChangeListener进度条监听------------------------*/
    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        Logger.d("BBBBB:" + progress);
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        Logger.d("AAAAA:" + seekBar.getProgress());
        point = seekBar.getProgress();
    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        Logger.d("CCCCC:" + seekBar.getProgress());

        if (!totalTime.getText().equals("00:00") &&
                play_or_pause.getTag().toString().equals("1")) { // 当前已经加载音乐，且不处于暂停状态
            // 跳转到指定位置
            mMusicView.seekToMusic(seekBar.getProgress());
        } else {
//            seekBar.setProgress(point);
        }
    }
}
